package bbs.net;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bbs.model.Submit;


/**
 * Created by nimi on 2015/11/17.
 */
public class GetSubmitBySubject {
    Long subjectID;
    Download download;
    Delegate delegate;
    public  GetSubmitBySubject(Long subjectID){
        this.subjectID=subjectID;
    }


    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public Delegate getDelegate() {
        return delegate;
    }

    public Download getDownload() {
        return download;
    }

    public void setDownload(Download download) {
        this.download = download;
    }

    public void get() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/BBS/GetAllBoardOfTopic";
        RequestQueue requestQueue = VolleySingleton.getVolleySingleton(MyApplication.share()).getRequestQueue();
        StringRequest stringRequest =new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            public void onResponse(String response) {
                afterGet(response);
                if(delegate!=null)
                    delegate.onResponse(response,GetSubmitBySubject.this);
            }}, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error, GetSubmitBySubject.this);

            }
        })

        {
            @Override
            protected Map<String, String> getParams () {
                Map map = new HashMap();
                map.put("data",subjectID+"");
                return map;
            }
            @Override
            protected Response<String> parseNetworkResponse (NetworkResponse response){
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }

        };
        requestQueue.add(stringRequest);
    }

    private void afterGet(String response) {
        download = JSON.parseObject(response, Download.class);
    }
    // 下行数据
    public static class Download {
        // 状态
        int status;//1 成功 2 失败
        // 主题
        List<Submit> bbs_boards = new ArrayList<Submit>();

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public List<Submit> getBbs_boards() {
            return bbs_boards;
        }

        public void setBbs_boards(List<Submit> bbs_boards) {
            this.bbs_boards = bbs_boards;
        }

    }
    public static interface Delegate{
        public void onResponse(String response, GetSubmitBySubject getSubmitBySubject);
        public void onErrorResponse(VolleyError error, GetSubmitBySubject getSubmitBySubject);
    }
}
